package com.qing;


import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class Demo4 {
	public static void main(String[] args) {
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("name","张三");
		jsonObject.put("age",12);


		User user=jsonToObject(jsonObject,User.class);
		System.out.println(user.getName());
		System.out.println(user.getAge());


		List<Integer> numbers=Arrays.asList(1,2,3,4,5,6);
		Map<Boolean,List<Integer>> map=numbers.stream().collect(Collectors.partitioningBy(item->item%2==0));
		for (Integer integer : map.get(true)) {
			System.out.println(integer);
		}

	}

	/**
	 * json转Object
	 * @param jsonObject
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> T  jsonToObject(JSONObject jsonObject, Class<T> clazz) {
		try {
			T t=(T)clazz.newInstance();
			Arrays.stream(clazz.getDeclaredFields()).forEach(item->
			{
				item.setAccessible(true);
				try {
					item.set(t,jsonObject.get(item.getName()));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			});
			return  t;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return null;
	}
}
